﻿using System;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using ThapCam.Ulti;
using System.Text;
using System.Xml;
using System.Dynamic;
using System.Data;
using MySql.Data.MySqlClient;


namespace ThapCam.System
{
    public static class ThapCamLogic
    {
        private static ThapCamApplication application;
        private static XmlManager thapCamConfig;

        public static Dictionary<string, object> properties;

        public static void createInstance(ThapCamApplication app)
        {
            properties = new Dictionary<string, object>();
            application = app;
            initData();
        }

        private static void loadXMLResource(string keyPath)
        {
            thapCamConfig = XmlManager.loadXML(application.Server.MapPath(keyPath));
        }

        private static void initData()
        {
            loadXMLResource("Configs/ThapCamConfig.xml");
            LogManager.createInstance();
            WebConfig.initConfig();

            initTemplate();
            initializeHeader();

        }

        #region initializeTemplate

        private static void initTemplate()
        {
            initHeaderMenu();
            initFooterMenu();
        }

        private static void initHeaderMenu()
        {
            StringBuilder str = new StringBuilder();
            XmlNode headerMenu = thapCamConfig["Config"]["Template"]["Header"]["HeaderMenu"];
            if (headerMenu != null && headerMenu.HasChildNodes)
            {
                string name, url;
                int length = headerMenu.ChildNodes.Count;              
                foreach (XmlNode node in headerMenu.ChildNodes)
                {
                  
                    name = node.Attributes["name"].Value;
                    url = WebConfig.HOST_URL + node.Attributes["url"].Value;
                    if (node.ChildNodes.Count == 0)
                        str.AppendLine("<li class=\"dropdown\"><a class=\"dropdown-toggle\" href=\"" + url + "\">" + name + "</a></li>");
                    else
                    {
                        str.AppendLine("<li class=\"dropdown\"><a class=\"dropdown-toggle\" data-toggle=\"dropdown\" href=\"javascript:;\">" + name + " <i class=\"fa fa-caret-down\"></i></a>");
                        str.AppendLine("<ul class=\"dropdown-menu\"><li><div class=\"yamm-content\"><div class=\"row\"><div class=\"col-lg-12 col-md-12 col-sm-12 col-xs-12\"><ul class=\"list-unstyled\">");
                        foreach (XmlNode item in node.ChildNodes)
                        {
                            name = item.Attributes["name"].Value;
                            url = WebConfig.HOST_URL + item.Attributes["url"].Value;
                            str.AppendLine("<li><a href=\"" + url + "\">" + name + "</a></li>");
                        }
                        str.AppendLine("</ul></div></div></div></li></ul></li>");
                    }
                }
            }
            properties.Add("HeaderMenu", str.ToString());
        }


        private static void initFooterMenu()
        {            
            StringBuilder str = new StringBuilder();
            XmlNode headerMenu = thapCamConfig["Config"]["Template"]["Footer"]["FooterMenu"];           
            if (headerMenu != null && headerMenu.HasChildNodes)
            {
                string name, url;
                int count = 0;
                int length = headerMenu.ChildNodes.Count;
                foreach (XmlNode node in headerMenu.ChildNodes)
                {
                    name = node.Attributes["name"].Value;
                    url = WebConfig.HOST_URL + node.Attributes["url"].Value;
                    count++;
                    if (count < length)
                        str.AppendLine("<a href=\"" + url + "\"><strong>" + name + "</strong></a><span>|</span>");
                    else
                        str.AppendLine("<a href=\"" + url + "\"><strong>" + name + "</strong></a>");

                    
                }
            }           
            properties.Add("FooterMenu", str.ToString());
        }


        #endregion

        #region initConfigHeader

        private static void initializeHeader()
        {
            initHeaderConfig("Default");           
        }

        private static void initHeaderConfig(string pageName)
        {
            dynamic obj = new ExpandoObject();
            string group = thapCamConfig["Config"]["HeaderData"][pageName]["group"].InnerText;
            string title = thapCamConfig["Config"]["HeaderData"][pageName]["title"].InnerText;
            string description = thapCamConfig["Config"]["HeaderData"][pageName]["descriptions"].InnerText;
            string keyword = thapCamConfig["Config"]["HeaderData"][pageName]["keyWords"].InnerText;
            obj.group = group;
            obj.title = title;
            obj.description = description;
            obj.keyword = keyword;
            properties.Add("HC" + pageName, obj);
        }

        #endregion

    }
}